package neu.edu.vn.appmusic.ui;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import neu.edu.vn.appmusic.R;
import neu.edu.vn.appmusic.adapter.MainViewPagerAdapter;
import neu.edu.vn.appmusic.utils.ToastUtils;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBar.TabListener;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends ActionBarActivity implements TabListener {
	
	private ActionBar actionBar;
	private ViewPager viewPager;
	private MainViewPagerAdapter viewPagerAdaper;
	private int[] icons_bar = {R.drawable.ic_music_online,R.drawable.ic_music_offline};
	private int[] titles = {R.string.fragment_name_online,R.string.fragment_name_offline};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		actionBar = getSupportActionBar();
//		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		actionBar.setIcon(R.drawable.default_music);
		
		viewPagerAdaper = new MainViewPagerAdapter(getSupportFragmentManager());
		viewPager = (ViewPager) findViewById(R.id.mainPager);
		viewPager.setAdapter(viewPagerAdaper);
		viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
			@Override
			public void onPageSelected(int position) {
				actionBar.setSelectedNavigationItem(position);
			}
		});
		
		for (int i = 0; i < viewPagerAdaper.getCount(); i++) {
			actionBar.addTab(
					actionBar.newTab()
					.setText(titles[i])
					.setIcon(icons_bar[i])
					.setTabListener(this));
		}
//		
//		SlidingMenu menu = new SlidingMenu(MainActivity.this);
//		menu.attachToActivity(this, SlidingMenu.SLIDING_WINDOW);
	}
	
	public void onClickAlbum(View view){
		Intent intent = new Intent(MainActivity.this, ActivityAlbum.class);
		startActivity(intent);
	}



	@Override
	public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void onTabSelected(Tab tab, FragmentTransaction arg1) {
		// TODO Auto-generated method stub
		viewPager.setCurrentItem(tab.getPosition());
		actionBar.setTitle(getResources().getString(titles[tab.getPosition()]));
	}



	@Override
	public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
		// TODO Auto-generated method stub
	
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.activity_main_actions, menu);
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		return super.onOptionsItemSelected(item);
	}
	
	

	

}
